import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

public class TestSerializationExternalizable {

	public void serialize(Employee emp) {
		try {
			OutputStream fos = new FileOutputStream("D:/Ramesh/ser");
			ObjectOutputStream os = new ObjectOutputStream(fos);
			os.writeObject(emp);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Employee deSerialize() {
		Employee emp = null;
		try {
			InputStream fis = new FileInputStream("D:/Ramesh/ser");
			ObjectInputStream os = new ObjectInputStream(fis);
			emp = (Employee) os.readObject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return emp;
	}

	public static void main(String[] args) {
		TestSerializationExternalizable test = new TestSerializationExternalizable();
		// Employee emp = new Employee("ramesh", 26, EmployeeType.PERMANENT,
		// 30000);
		Employee emp = new WaseEmployee();
		emp.setName("Ramesh");
		test.serialize(emp);
		Employee emp1 = test.deSerialize();
		emp1.work();
		// System.out.println("Emp: "+ emp1.getName() + " "+ emp1.getAge() +
		// " "+ emp1.getType().toString()+ " "+ emp1.getSalary());

	}

}

enum EmployeeType {
	PERMANENT, CONTRACT, WASIAN;
}

class Employee {
	/**
	 * 
	 */
	// private static final long serialVersionUID = -8814847317422973958L;
	String name;
	int age;
	EmployeeType type;
	int salary;

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}

	/**
	 * @param age
	 *            the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}

	/**
	 * @return the type
	 */
	public EmployeeType getType() {
		return type;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(EmployeeType type) {
		this.type = type;
	}

	/**
	 * @return the salary
	 */
	public int getSalary() {
		return salary;
	}

	/**
	 * @param salary
	 *            the salary to set
	 */
	public void setSalary(int salary) {
		this.salary = salary;
	}

	public void work() {
		System.out.println("job");
	}

	/**
	 * 
	 */
	public Employee() {
		super();
	}

	/**
	 * @param name
	 * @param age
	 * @param type
	 * @param salary
	 */
	public Employee(String name, int age, EmployeeType type, int salary) {
		super();
		this.name = name;
		this.age = age;
		this.type = type;
		this.salary = salary;
	}

	private void writeObject(java.io.ObjectOutputStream out) throws IOException {
		System.out.println("Inside writeObject");
		out.defaultWriteObject();
	}

	// public Object writeReplace() throws ObjectStreamException {
	// System.out.println("Inside writeReplace..");
	// return null;
	// }

}

class WaseEmployee extends Employee implements Serializable {

	public WaseEmployee() {
		super();
		System.out.println("Wase constructor..");
	}

	@Override
	public void work() {
		System.out.println("Study + job");
	}
	/*
	 * @Override public void readExternal(ObjectInput in) throws IOException,
	 * ClassNotFoundException { // TODO Auto-generated method stub
	 * System.out.println("Inside readExternal.."); String name =
	 * (String)in.readObject(); System.out.println("name = "+name); }
	 * 
	 * @Override public void writeExternal(ObjectOutput out) throws IOException
	 * { // TODO Auto-generated method stub
	 * System.out.println("Inside writeExternal..");
	 * 
	 * out.writeObject(name); }
	 */

}

class PermanentEmployee extends Employee implements Serializable {

	public void work() {
		System.out.println("Permanent job");
	}

}

class ContractualEmployee extends Employee {

	public void work() {
		System.out.println("Temporary job");
	}

}
